package com.upu173.qrcode.core;

import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.Log;
import com.andbase.library.utils.AbLogUtil;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.PlanarYUVLuminanceSource;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.ResultPoint;
import com.google.zxing.common.GlobalHistogramBinarizer;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.upu173.qrcode.camera.ResultPointCallback;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

public class DecodeUtil {

    public static List<ResultPoint> pointList = new ArrayList<>();

    public static  String  decode(Bitmap bitmap) {
        try {
            Hashtable<DecodeHintType, Object> hints = new Hashtable<>();
            hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
            hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);

            int[] bitmapData = new int[bitmap.getWidth() * bitmap.getHeight()];
            bitmap.getPixels(bitmapData, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

            RGBLuminanceSource source = new RGBLuminanceSource(bitmap.getWidth(), bitmap.getHeight(),bitmapData);
            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
            QRCodeReader qrCodeReader = new QRCodeReader();
            Result result = qrCodeReader.decode(binaryBitmap, hints);
            String code = result.getText();
            AbLogUtil.e("DecodeThread", "扫描到了二维码：" + code);
            return  code;
        } catch (Exception e) {
            e.printStackTrace();
            AbLogUtil.e("DecodeThread", "扫描二维码异常：" + e.getMessage());
        }
        return  "";
    }

    public static String decode(byte[] yuvData, int width, int height, Rect frameRect,final ResultPointCallback callback) {
        pointList.clear();
        Result result = null;
        try {
            Log.e("DecodeThread", "开始识别二维码...");
            Hashtable<DecodeHintType, Object> hints = new Hashtable<>();
            //hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
            hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
            hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);
            hints.put(DecodeHintType.NEED_RESULT_POINT_CALLBACK, new com.google.zxing.ResultPointCallback() {
                @Override
                public void foundPossibleResultPoint(ResultPoint point) {
                    pointList.add(point);
                    callback.foundResultPoints(pointList);
                    AbLogUtil.i("foundResultPoints","resultPoints:" + pointList.size());
                }
            });


            PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(yuvData,
                    width,
                    height,
                    (width-frameRect.height())/2,(height - frameRect.width())/2
                    ,frameRect.height(),
                    frameRect.width(),
                    false);

            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
            QRCodeReader qrCodeReader = new QRCodeReader();
            try {
                result = qrCodeReader.decode(binaryBitmap, hints);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                qrCodeReader.reset();
            }

            if(result!=null){
                String code = result.getText();
                AbLogUtil.e("DecodeThread","扫描到了二维码：" + code);
                return  code;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  null;

    }
}
